/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sai.conexion;

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;

/**
 *
 * @author PMX-Java
 */
public class PoolConexion {private static PoolConexion instance = null;
    private BasicDataSource dataSource;
    private PoolConexion() {
        
        dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/websai");
        dataSource.setUsername("root");
        dataSource.setPassword("");
    }

    public static PoolConexion getInstance() {
        if(instance == null) {
            instance = new PoolConexion();
        }
        return instance;
    }

    public Connection getConexion() {
        Connection conex = null;
        try {
            conex = dataSource.getConnection();
        } catch (SQLException ex) {
            
        }
        return conex;
    }

    public void cerrarConexion(Connection conexion) {
        try {
            if (conexion != null && !conexion.isClosed()) {
                conexion.close();
            }
        } catch (SQLException ex) {
            
        }
    }
    
    public void beginTransaction(Connection conex) {
        try {
            if (conex != null && !conex.isClosed()) {
                conex.setAutoCommit(false);
            }
        } catch(SQLException ex) {
            
        }
    }

    public void endTransaction(Connection conex) {
        try {
            if (conex != null && !conex.isClosed()) {
                conex.setAutoCommit(true);
            }
        } catch(SQLException ex) {
            
        }
    }

    public void commit(Connection conex) {
        try {
            if (conex != null && !conex.isClosed()) {
                conex.commit();
            }
        } catch(SQLException ex) {
            
        }
    }

    public void rollback(Connection conex) {
        try {
            if (conex != null && !conex.isClosed()) {
                conex.rollback();
            }
        } catch(SQLException ex) {
            
        }
    }
}
